package com.tucc.hello.leetcode.no1_400.no1_200.no61_80.no65;

/**
 * @author tucc
 * @description todo
 * @package com.tucc.hello.leetcode
 * @date 2021/5/20
 **/
public class Solution {

    public static void main(String[] args) {
        Solution solution = new Solution();
//        String[] right = {"2", "0089", "-0.1", "+3.14", "4.", "-.9", "2e10", "-90E3", "3e+7", "+6e-1", "53.5e93", "-123.456e789"};
//        System.out.println("======true=====");
//        for (String s : right) {
//            System.out.println(solution.isNumber(s));
//        }
//        String[] bad = {"e", ".", "abc", "1a", "1e", "e3", "99e2.5", "--6", "-+3", "95a54e53"};
//        System.out.println("======false=====");
//        for (String s : bad) {
//            System.out.println(solution.isNumber(s));
//        }

        System.out.println(solution.isNumber("7e69e"));
    }

    /**
     * 部分有效数字列举如下：
     * <p>
     * ["2", "0089", "-0.1", "+3.14", "4.", "-.9", "2e10", "-90E3", "3e+7", "+6e-1", "53.5e93", "-123.456e789"]
     * 部分无效数字列举如下：
     * <p>
     * ["abc", "1a", "1e", "e3", "99e2.5", "--6", "-+3", "95a54e53"]
     *
     * @param s
     * @return
     */
    public boolean isNumber(String s) {
        if (s.length() > 0) {
            char first = s.charAt(0);
            if(first == 'e' || first == 'E'){
                return false;
            }
            char last = s.charAt(s.length() - 1);
            if(last == 'e' || last == 'E'){
                return false;
            }
            String[] ss = s.split("(?i)e");
            if (ss.length > 2 || ss.length == 0) {
                return false;
            }
            if (ss.length == 1) {
                return isNumber(s, true, true);
            }
            return isNumber(ss[0], true, true) && isNumber(ss[1], true, false);
        }
        return false;
    }


    public boolean isNumber(String s, boolean sign, boolean point) {
        if (s.length() > 0) {
            char ch = s.charAt(0);
            if ((ch == '+' || ch == '-')) {
                return sign && isNumber(s.substring(1), false, true);
            }
            if (ch == '.') {
                return point && isNumber(s.substring(1), false, false);
            }
            for (int i = 0; i < s.length(); i++) {
                ch = s.charAt(i);
                if (ch < '0' || ch > '9') {
                    if (ch == '.' && point) {
                        point = false;
                        continue;
                    }
                    return false;
                }
            }
            return true;
        }
        return false;
    }
}
